<template>
	<view>
		<block v-if="dataBuffer">
			<view class="item" 
				v-for="(res, index) in siteList" :key="res.id" 
			>	<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id" @click="delAdd"></u-icon>
				<view class="top" @click="changeAddress(index)">
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag">
						<text class="red" v-if="res.status == 1">默认</text>
					</view>
				</view>
				<view class="bottom" >
					{{res.location+res.useraddress}}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="index" @click="editAddress"></u-icon>
				</view>
			</view>
		</block>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			siteList: [],//初始化地址数据
			dataBuffer:true
		};
	},
	onShow() {
		this.getAddress()
	},
	methods: {
		// 选择收货地址
		changeAddress(index){
			let address = this.siteList[index];
			uni.setStorageSync('address',address)
			uni.navigateTo({
				url:'../order/order'
			})
		},
		// 跳转到新增地址页面
		toAddSite(){
			uni.navigateTo({
				url:'./addSite'
			})
		},
		// 修改地址
		editAddress(index){
			// console.log( this.siteList[index] );
			let cart = this.siteList[index]
			uni.setStorageSync('address',cart)
			uni.navigateTo({
				url:'../address/addSite?type=1'  // type 标识
			})
		},
		// 删除地址
		delAdd(index){
			// console.log(index)
			let {token} = uni.getStorageSync('userinfo')
			uni.showModal({
				title:'重要提醒',
				content:'你是否要删除收货地址',
				success: async (res) => {
					if(res.confirm){
						let options = {
							url:'/api/addressremove',
							method:'POST',
							data:{id:index},
							header:{
								authorization:token
							}
						}
						let res = await this.$http(options)
						if(res.data.code==200){
							uni.showToast({
								title:'删除成功',
								icon:'success'
							})
							this.getAddress()
							console.log(res);
						}else if(res.data.code == 403){
							uni.showToast({
								title:'登录失效',
								icon:'error'
							})
							setTimeout(()=>{
								uni.navigateTo({
									url:'../login/login'
								})
							},2000)
						}
					}else{
						uni.showToast({
							title:'不删除了',
							icon:'error'
						})
					}
				}
			})
			
		},
		// 获取收货地址列表
		async getAddress(){
			let {token,uid} = uni.getStorageSync('userinfo')
			let options = {
				url:'/api/addresslist',
				data:{uid},
				header:{
					authorization:token
				}
			}
			let res = await this.$http(options)
			if(res.data.code == 200){
				console.log(res.data.list)
				if(res.data.list == null){
					uni.showToast({
						title:'还没有收货地址',
						icon:'error'
					})
					this.dataBuffer = false
					return
				}
				this.siteList = res.data.list
				this.dataBuffer = true
			}else if(res.data.code == 403){
				uni.showToast({
					title:'登录失效',
					icon:'error'
				})
				setTimeout(()=>{
					uni.navigateTo({
						url:'../login/login'
					})
				},2000)
			}else{
				uni.showToast({
					title:'网络故障~',
					icon:'error'
				})
			}
		}
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/address.scss';
</style>
